import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;


public class FileRead {
	private Scanner scanner = null;
	private Data data = null;
	private ArrayList<Data> listOfData;
	private String filename;
	
	/**
	 * To read the file and store the examples into two dimension ArralyList
	 * @param filename
	 */
	public FileRead(String filename){
		this.filename = filename;
		try {
			listOfData = new ArrayList<Data>();
			scanner = new Scanner(new File(filename));
			while (scanner.hasNextLine()) {
				data = new Data();
				String line = scanner.nextLine();
				String[] attributesFromText = line.split(",\\s*");
				for (String attribute : attributesFromText){
					data.addAttribute(attribute);
				}
				listOfData.add(data);				
			}
		} catch (FileNotFoundException e) {
			System.err.println("Cannot find the file");
		} finally {
			if (scanner != null)
				scanner.close();
		}
	}
	
	/**
	 * To get the data read from the file
	 * @return
	 */
	public ArrayList<Data> getDataList(){
		return listOfData;
	}
	
	/**
	 * This is to convert test data to the same format, in order to test each attribute and go down to the leaf node
	 * @param converted
	 * @return
	 */
	public ArrayList<Data> convertExamples(SelectSplitPoint converted){
		SelectSplitPoint convertTrainingData = new SelectSplitPoint(filename);
		
			ArrayList<Double> splitPointVacation = converted.getPerfectPoints(InitialAttribute.Vacation);
			//ArrayList<Data> vacation = convertTrainingData.convert(listOfData, splitPointVacation.get(0),splitPointVacation.get(1), InformationGain.Vacation);
			ArrayList<Data> vacation = convertTrainingData.convert(listOfData, splitPointVacation.get(0),splitPointVacation.get(1),splitPointVacation.get(2), InitialAttribute.Vacation);	
			
			ArrayList<Double> splitPointECredit = converted.getPerfectPoints(InitialAttribute.eCredit);
			//ArrayList<Data> eCredit = convertTrainingData.convert(vacation, splitPointECredit.get(0),splitPointECredit.get(1), InformationGain.eCredit);
			ArrayList<Data> eCredit = convertTrainingData.convert(vacation, splitPointECredit.get(0),splitPointECredit.get(1),splitPointECredit.get(2), InitialAttribute.eCredit);
			
			ArrayList<Double> splitPointSalary = converted.getPerfectPoints(InitialAttribute.Salary);
			//ArrayList<Data> salary = convertTrainingData.convert(eCredit, splitPointSalary.get(0),splitPointSalary.get(1), InformationGain.Salary);
			ArrayList<Data> salary = convertTrainingData.convert(eCredit, splitPointSalary.get(0),splitPointSalary.get(1),splitPointSalary.get(2), InitialAttribute.Salary);
			
			ArrayList<Double> splitPointProperty = converted.getPerfectPoints(InitialAttribute.Property);
			//ArrayList<Data> property = convertTrainingData.convert(salary, splitPointProperty.get(0),splitPointProperty.get(1), InformationGain.Property);
			ArrayList<Data> property = convertTrainingData.convert(salary, splitPointProperty.get(0),splitPointProperty.get(1),splitPointProperty.get(2), InitialAttribute.Property);
			return property;
			
		
	}
}
